﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using NHibernate.Validator.Engine;
using YoProgramo.Domain.BusinessLogic.Service;
using YoProgramo.Domain.Core.Interfaces;
using YoProgramo.Domain.Core.MemberShip;

namespace YoProgramo.BusinessLogic.Service
{
    public class MemberShipService : IMembershipService {
        private IUserService _UserService;
        private IUserRepository _UserRespository;

        public MemberShipService(IUserService userService, IUserRepository userRepository) {
            _UserService = userService;
            _UserRespository = userRepository;
        }

        public int MinPasswordLength {
            get {
                return 5;
                //ver esto
            }
        }

        public bool ValidateUser(string userName, string password) {
            if (string.IsNullOrEmpty(userName) ||
                string.IsNullOrEmpty(password)) 
                return false;

            IUser user = _UserRespository.FindByUserNameAndPassword(userName, password);
            if (user != null) return true;

            return false;
        }


        public InvalidValue[] CreateUser(string userName, string password, string email) {
            return  _UserService.Create(userName, password, email);    
        }

        public bool ChangePassword(string userName, string oldPassword, string newPassword) {
            try
            {
                IUser user = _UserRespository.FindByUserNameAndPassword(userName, oldPassword);
                user.Password = newPassword;
            }catch
            {
                return false;
            }
            return true;
        }
    }
}